package me.w70.fps.map;

//import java.net.MalformedURLException;
//import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import me.w70.fps.FPS;
//import me.w70.fps.map.MapGen;

public class Map {
	// Constant
	public final int TILE_SIZE = 64;
	protected List<Door> doors = new ArrayList<Door>();
	
	// True = doors open left-to-right
	// False = doors open right-to-left
	private boolean doorsopenleft = true;
	
	protected Cell[] map;
	
	protected char[] CharMap = { 
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','8','8','8','8','8','8','8','8','8','8','8','8','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','8','8','8','9','8','8','8','8','8','9','8','8','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','8','0','0','0','0','0','0','0','0','0','0','0','8','8','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','0','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','7','1','1','1','9','0','0','0','0','0','0','0','0','0','0','0','9','8','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','0','0','0','0','1','1','1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','0','8','0','0','0','0','0','0','0','0','0','0','0','8','0','0','0','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','0','0','0','0','0','1','1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','0','|','0','0','0','0','0','0','0','0','0','0','0','|','0','0','0','9','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','0','0','0','0','0','1','1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','0','8','0','0','0','0','0','0','0','0','0','0','0','8','0','0','0','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','6','1','1','5','1','1','6','1','1','1','1','0','0','0','1','1','1','7','1','1','1','A','0','0','0','0','0','0','0','0','0','0','0','A','8','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','1','1','1','0','0','0','1','1','1','1','1','1','1','8','0','0','0','0','0','0','0','0','0','0','0','8','8','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','7','0','0','0','0','0','0','0','0','0','7','1','1','0','0','0','1','1','1','1','1','1','1','8','8','8','9','8','8','|','8','8','9','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','1','0','0','0','0','0','0','0','1','1','1','1','1','8','8','8','8','8','0','0','0','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','|','0','0','0','0','0','0','0','1','1','1','1','1','1','1','8','8','8','0','0','0','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','1','0','0','0','0','0','0','0','1','1','1','1','1','1','1','8','8','A','0','0','0','A','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','7','0','0','0','0','0','0','0','0','0','7','1','1','1','6','1','1','1','1','1','1','1','1','1','1','8','8','8','0','0','0','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','0','0','0','1','1','0','0','0','0','0','0','0','0','0','1','1','1','1','1','1','1','1','1','1','1','8','8','8','8','8','8','8','0','0','0','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','0','0','0','1','1','1','6','1','1','|','1','1','6','1','1','1','1','1','1','1','1','1','1','1','1','8','8','8','8','8','8','8','0','0','0','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','0','0','0','1','1','1','1','1','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','8','8','0','0','9','0','0','0','0','0','0','0','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','|','1','1','1','1','1','7','0','0','0','7','1','1','1','1','1','1','1','1','1','1','1','1','1','8','8','0','0','8','8','8','0','0','0','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','0','0','0','1','1','1','1','1','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','8','8','0','0','8','8','8','0','0','0','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','0','0','0','1','0','0','0','1','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','8','8','0','0','8','8','8','0','0','0','8','8','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','0','0','0','0','0','0','0','7','0','0','0','7','1','1','1','1','1','1','1','1','1','1','1','1','1','8','8','8','8','8','8','9','0','0','0','9','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','0','0','0','1','0','0','0','1','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','8','8','8','8','8','8','8','0','0','0','8','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','2','2','2','2','2','2','2','2','2','2',
			'1','0','0','0','1','1','1','1','1','1','|','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','8','9','|','9','8','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','2','2','2','2','2','2','2','2','2','2',
			'7','0','0','0','7','1','0','0','0','0','0','0','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','7','1','p','0','0','0','p','1','7','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','0','0','0','0','0','2','2',
			'1','0','0','0','1','1','0','0','0','0','0','0','0','0','0','7','1','1','1','1','1','1','1','1','1','1','1','p','0','G','0','0','0','0','0','0','0','0','0','0','0','p','2','2','2','2','2','2','2','2','2','2','2','2','0','0','0','0','0','0','0','0','2','2',
			'1','0','0','0','1','1','0','0','0','0','0','0','0','0','0','1','1','1','1','1','1','1','1','1','1','1','6','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','6','2','2','2','2','2','2','2','2','2','2','2','0','0','0','0','0','0','0','0','2','2',
			'1','0','0','0','0','1','0','0','0','0','0','0','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2','0','0','0','0','0','0','0','0','0','2','0','0','0','0','0','0','0','0','2','2',
			'1','0','0','0','0','|','0','0','0','0','0','0','0','0','0','7','1','1','1','1','1','1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','|','0','0','0','0','0','0','0','0','0','|','0','0','0','0','0','0','0','0','4','2',
			'1','0','0','0','0','1','0','0','0','0','0','0','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2','0','G','0','0','0','0','0','0','0','2','0','0','0','0','0','0','0','0','2','2',
			'1','0','0','0','1','1','0','0','0','0','0','0','0','0','0','1','1','1','1','1','1','1','1','1','1','1','6','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','6','2','2','2','0','0','0','0','2','2','2','2','0','0','0','0','0','0','0','0','2','2',
			'1','0','0','0','1','1','0','0','0','0','0','0','0','0','0','7','1','1','1','1','1','1','1','1','1','1','1','p','0','0','0','0','0','0','0','0','0','0','0','0','0','p','1','1','1','2','2','0','0','2','2','2','2','2','0','0','0','0','0','0','0','0','2','2',
			'1','0','0','0','1','1','0','0','0','0','0','0','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','5','1','1','1','0','0','0','0','0','1','1','1','1','1','1','1','1','2','2','0','0','2','2','2','2','2','0','0','0','0','0','0','0','0','2','2',
			'5','0','0','0','5','1','1','1','1','1','|','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','2','|','2','2','2','1','1','1','1','1','1','1','2','2','0','0','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2',
			'1','0','0','0','1','1','1','1','1','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','2','2','1','1','1','1','1','1','1','2','2','0','0','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2',
			'1','0','0','0','1','1','1','1','1','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','2','2','1','1','1','1','1','1','1','2','2','0','0','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2',
			'1','0','0','0','1','0','0','0','1','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','2','2','1','1','1','1','1','1','1','2','2','0','0','2','2','2','2','2','2','2','2','2','2','2','2','2','1','1',
			'1','0','0','0','0','0','0','0','7','0','0','0','7','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','2','2','1','1','1','1','1','1','1','2','2','0','0','2','2','2','0','2','0','2','0','2','0','2','2','2','1','1',
			'1','0','0','0','1','0','0','0','1','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','2','2','1','1','1','1','1','1','1','2','2','0','0','0','2','0','0','0','0','0','0','0','0','0','2','2','1','1',
			'1','0','0','0','1','1','1','1','1','0','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','2','2','1','1','1','1','1','1','1','2','2','0','0','0','|','0','0','0','0','0','0','0','0','0','4','2','1','1',
			'1','0','0','0','1','1','1','1','1','0','0','0','1','1','1','1','1','7','1','1','1','6','1','1','1','1','1','1','1','1','1','2','2','0','0','0','2','2','1','1','1','1','1','1','1','2','2','0','0','0','2','0','0','0','0','0','0','0','0','0','2','2','1','1',
			'1','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','0','0','0','0','0','0','7','1','1','1','1','1','1','2','2','0','0','0','2','2','1','1','1','1','1','1','1','2','2','2','2','2','2','2','0','2','0','2','0','2','0','2','2','2','1','1',
			'1','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','|','0','0','0','0','0','0','|','0','E','1','1','1','1','2','2','0','0','0','2','2','1','1','1','1','1','1','1','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','1','1',
			'1','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','0','0','0','0','0','0','7','1','1','1','1','1','1','2','2','0','0','0','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','5','1','1','1','1','1','1','1','1','1','1','6','1','1','1','1','1','1','2','2','2','2','2','0','0','0','2','2','2','2','2','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','0','0','1','0','0','1','1','1','1','1','1','1','2','2','2','2','2','2','2','|','2','2','2','2','2','2','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','0','|','0','0','1','0','0','1','0','0','1','1','1','1','1','1','2','2','0','0','0','0','2','0','0','0','2','0','0','0','0','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','0','0','0','0','0','1','1','1','1','1','1','1','1','1','2','2','0','0','0','0','|','0','0','0','|','0','0','0','0','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','0','2','0','0','0','2','0','0','0','0','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','0','2','0','0','0','2','0','0','0','0','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1','1','1','1','1','1','1','2','2','2','2','2','2','2','0','0','0','2','2','2','2','2','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','0','2','0','0','0','2','0','0','0','0','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','0','|','0','0','0','|','0','0','0','0','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','0','2','0','0','0','2','0','0','0','0','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','2','2','2','2','2','0','0','0','2','2','2','2','2','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
			'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','3','2','2','3','2','2','4','2','2','3','2','2','3','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'
			 };
	// List map dimenations.
	protected int mapx = 64;
	protected int mapy = 64;
	// Map start position.
	protected int StartX = 30;
	protected int StartY = 58;
	// Map cell to Door mappings.
	public Hashtable<Integer, Door> doormap = new Hashtable<Integer, Door>();
	// Getters + setters.
	public int getStartX() { return StartX; }
	public void setStartX(int startX) { StartX = startX; }
	public int getStartY() { return StartY; }
	public void setStartY(int startY) { StartY = startY; }
	public char[] getCharMap() { return CharMap; }
	public int getMapx() { return mapx; }
	public void setMapx(int mapX) { mapx = mapX; }
	public int getMapy() { return mapy; }
	public void setMapy(int mapY) { mapy = mapY; }
	
	public Cell[] getMap() { return this.map; }

	public void createMap(char[] charmap, int mapx, int mapy) {
		Cell[] cellmap = new Cell[charmap.length];
		for (int i = 0; i < charmap.length; i++) {
			cellmap[i] = new Cell(charmap[i], (byte) 0);
		}
		cellmap[this.StartX + (this.StartY * this.mapx)].lightlevelmod = 20;
		this.map = cellmap;
		this.mapx = mapx;
		this.mapy = mapy;
	}
	
	// Initialize all doors on the map.
	public void loadDoors() {
		for (int i = 0; i < map.length; i++) {
			if (map[i].mapchar == '|') {
				Door thedoor = new Door();
				this.doormap.put(i, thedoor);
			}
		}
	}
	
	// Update all the doors on the map.
	public void updateDoors() {
		for (Door d : this.doormap.values()) {
			// Door is either waiting to close, or is in the process of closing/opening
			if (d.status != 0) {
				if (d.status == 1) {
					if (this.doorsopenleft) {
						d.frames += 4;
					}
					else {
						d.frames -= 4;
					}
				}
				else if (d.status == 2) {
					d.opencounter -= 2;
				}
				else if (d.status == 3) {
					if (this.doorsopenleft) {
						d.frames -= 4;
					}
					else {
						d.frames += 4;
					}
				}
				// Door just finished opening.
				if (Math.abs(d.frames) == 64 && d.status == 1) {
					d.status = 2;
					d.opencounter = 100;
				}
				// Door just finished closing
				else if (d.frames == 0 && d.status == 3) {
					d.status = 0;
				}
				// Door is open and has reached its peak open time
				else if (d.opencounter == 0 && d.status == 2) {
					d.status = 3;
				}
			}
		}
	}
	
	public Map(FPS applet) {
		/*MapGen generator = new MapGen(this);
		try {
			CharMap = generator.readfile(new URL(applet.getCodeBase(), "/level/e1m1.txt"));
		} 
		catch (MalformedURLException e) {
			e.printStackTrace();
		}
		generator.mapprintf(CharMap, charmapx);
		*/
		createMap(this.CharMap, 64, 64);
		this.loadDoors();
	}
}
